<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\RouteModel;
use cmf\controller\AdminBaseController;
use think\Db;

class RecycleBinController extends AdminBaseController
{
    /**
     * 回收站
     * @adminMenu(
     *     'name'   => '回收站',
     *     'parent' => '',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '回收站',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $param     = $this->request->param();
        $table_name = empty($param['table_name']) ? '' : $param['table_name'];

        $where = ['site_id' => get_site_id()];

        if (!empty($table_name)) {
            $where['table_name'] = $table_name;
        }

        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['create_time'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['create_time'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['create_time'] = ['<= time', $endTime];
            }
        }

        $keyword = empty($param['keyword']) ? '' : $param['keyword'];
        if (!empty($keyword)) {
            $where['name'] = ['like', "%$keyword%"];
        }

        $list = Db::name('recycleBin')->where($where)->order('create_time desc')->paginate(15)->appends($param);;
        // 获取分页显示
        $page = $list->render();

        $tableNameList = Db::name('recycleBin')->order('table_name asc')->column('table_name');
        $tableNameList = array_unique($tableNameList);
        foreach ($tableNameList as $k => $v) {
            $typeList[$v] = lang('TABLE_'.strtoupper($v));
        }
        // pre(html_options($typeList,));

        $this->assign('type_list', html_options($typeList, $table_name));
        
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('page', $page);
        $this->assign('list', $list);

        $actions = ['restore', 'delete'];
        $this->assign('actions', actions_check($this->request->module().'/'.$this->request->controller().'/', $actions));

        return $this->fetch();
    }

    /**
     * 回收站还原
     * @adminMenu(
     *     'name'   => '回收站还原',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '回收站还原',
     *     'param'  => ''
     * )
     */
    function restore()
    {

        $id     = $this->request->param('id');
        $result = Db::name('recycleBin')->where(['id' => $id])->find();

        $tableName = explode('#', $result['table_name']);
        $tableName = $tableName[0];

        //还原资源
        if ($result) {
            //主表数据删除时间清零
            if($tableName=='goods'){
                $fieldId = 'goods_id';
            }else{
                $fieldId = 'id';
            }
            $res = Db::name($tableName)
                ->where([$fieldId => $result['object_id']])
                ->update(['delete_time' => '0']);
            
            //设置关联表数据状态
            switch ($tableName) {
                //还原文章，并修改文章和分类关联表状态
                case 'portal_post':
                    Db::name('portal_category_post')->where('post_id',$result['object_id'])->update(['status'=>1]);
                    break;
                //还原基地文章，并修改文章和分类关联表状态
                case 'branch_post':
                    Db::name('branch_category_post')->where('post_id',$result['object_id'])->update(['status'=>1]);
                    break;
                //还原产品，并修改产品和分类关联表状态
                case 'goods':
                    Db::name('goods_category_relate')->where('goods_id',$result['object_id'])->update(['status'=>1]);
                    break;
                default:
                    # code...
                    break;
            }
            
            if ($res) {
                $re = Db::name('recycleBin')->where('id', $id)->delete();
                if ($re) {
                    $this->success("还原成功！");
                }
            }
        }
    }

    /**
     * 回收站彻底删除
     * @adminMenu(
     *     'name'   => '回收站彻底删除',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '回收站彻底删除',
     *     'param'  => ''
     * )
     */
    function delete()
    {
        $param = $this->request->param();

        if (isset($param['id'])) {
            $id     = $this->request->param('id');
            $result = Db::name('recycleBin')->where(['id' => $id])->find();

            //删除资源
            if ($result) {

                //页面没有单独的表.
                if($result['table_name'] === 'portal_post#page'){
                    $re = Db::name('portal_post')->where('id', $result['object_id'])->delete();
                    //消除路由
                    $routeModel = new RouteModel();
                    $routeModel->setRoute('', 'portal/Page/index', ['id' => $result['object_id']], 2, 5000);
                    $routeModel->getRoutes(true);
                }else{
                    if($result['table_name']=='goods'){
                        $fieldId = 'goods_id';
                    }else{
                        $fieldId = 'id';
                    }
                    $re = Db::name($result['table_name'])->where($fieldId, $result['object_id'])->delete();
                }

                if ($re) {
                    // 删除主表数据
                    $res = Db::name('recycleBin')->where('id', $id)->delete();

                    //删除关联表数据
                    switch ($result['table_name']) {
                        //删除文章，并修改文章和分类关联表状态
                        case 'portal_post':
                            Db::name('portal_category_post')->where('post_id',$result['object_id'])->delete();
                            break;
                        //删除基地文章，并修改文章和分类关联表状态
                        case 'branch_post':
                            Db::name('branch_category_post')->where('post_id',$result['object_id'])->delete();
                            break;
                        //删除产品，并修改产品和分类关联表状态
                        case 'goods':
                            Db::name('goods_category_relate')->where('goods_id',$result['object_id'])->delete();
                            break;
                        default:
                            # code...
                            break;
                    }

                    // if($result['table_name'] === 'portal_post'){
                        
                    // }
                    if ($res) {
                        $this->success("删除成功！");
                    }
                }
            }
        }

        if (isset($param['ids'])) {
            $ids = $this->request->param('ids/a');

            foreach ($ids as $v) {
                $id     = $v;
                $result = Db::name('recycleBin')->where(['id' => $id])->find();

                //删除资源
                if ($result) {

                    //页面没有单独的表.
                    if($result['table_name'] === 'portal_post#page'){
                        $re = Db::name('portal_post')->where('id', $result['object_id'])->delete();
                        //消除路由
                        $routeModel = new RouteModel();
                        $routeModel->setRoute('', 'portal/Page/index', ['id' => $result['object_id']], 2, 5000);
                        $routeModel->getRoutes(true);
                    }else{
                        if($result['table_name']=='goods'){
                            $fieldId = 'goods_id';
                        }else{
                            $fieldId = 'id';
                        }
                        $re = Db::name($result['table_name'])->where($fieldId, $result['object_id'])->delete();
                    }

                    if ($re) {
                        // 删除主表数据
                        $res = Db::name('recycleBin')->where('id', $id)->delete();

                        //删除关联表数据
                        switch ($result['table_name']) {
                            //删除文章，并修改文章和分类关联表状态
                            case 'portal_post':
                                Db::name('portal_category_post')->where('post_id',$result['object_id'])->delete();
                                break;
                            //删除基地文章，并修改文章和分类关联表状态
                            case 'branch_post':
                                Db::name('branch_category_post')->where('post_id',$result['object_id'])->delete();
                                break;
                            //删除产品，并修改产品和分类关联表状态
                            case 'goods':
                                Db::name('goods_category_relate')->where('goods_id',$result['object_id'])->delete();
                                break;
                            default:
                                # code...
                                break;
                        }

                        // if($result['table_name'] === 'portal_post'){
                            
                        // }
                    }
                }
            }
            
            $this->success("删除成功！");
        }
    }
}
